<?php
/**
 * Created by PhpStorm.
 * User: xuzhong
 * Date: 2025/4/3
 * Time: 11:55
 */
declare(strict_types=1);

namespace app\admin\service;

use app\admin\common\ServiceResponse;
use app\admin\model\Message as MessageModel;

class Message extends BaseService
{
    use ServiceResponse;

    protected $model;

    public function __construct()
    {
        $this->model = new MessageModel();
        parent::__construct();
    }

    /**
     * @describe 站内信列表
     * @author: 徐众
     * @date: 2025-04-02 13:02:26
     * @param [] type
     * @return Renderable
     */
    public function getList($where, $page, $limit): array
    {
        if (0 >= $page || 100 < $limit) {
            return $this->error(100002);
        }

        $total = $this->model->where($where)->count();
        if (!$total) {
            return $this->success(['list' => [], 'total' => 0]);
        }
        $list = $this->model->where($where)
            ->field('id,title,fullname,content,email,whatsapp,mtype,create_time,status')
            ->order('id DESC')
            ->page($page, $limit)
            ->select()->toArray();

        return $this->success(['list' => $list, 'total' => $total]);
    }


    /**
     * @describe 删除站内信
     * @author: 徐众
     * @date: 2025-04-02 13:02:26
     * @param [] type
     * @return Renderable
     */
    public function delete($param): array
    {

        #校验参数
        if (!isset($param['ids']) || empty($param['ids'])) {
            return $this->error(100001, '操作失败');
        }
        #查询数据是否存在
        $result = $this->model->where('id', 'in', $param['ids'])->where('is_del', '=', 0)->select()->toArray();

        if (!empty($result)) {
            #更新记录
            $update = $this->model->where('id', 'in', $param['ids'])->where('is_del', '=', 0)->update([
                'is_del' => time()
            ]);
            if ($update) {
                #尝试记录操作日志 不影响主流程
                try {
                    $this->logger->logOperation($param['ids'], 'Message', '删除', '删除站内信');
                } catch (\Exception $e) {
                    #记录日志或其他操作
                }
                return $this->success();
            } else {
                return $this->error(500001, '操作失败');
            }

        } else {
            #未找到记录返回失败
            return $this->error(400000, '未找到相关数据');
        }

    }
    /**
     * @describe 处理站内信
     * @author: 徐众
     * @date: 2025-04-02 13:02:26
     * @param [] type
     * @return Renderable
     */
    public function handle($param): array
    {
        #校验参数
        if (!isset($param['id']) || empty($param['id'])) {
            return $this->error(100001, '操作失败');
        }
        #查询数据是否存在
        $result = $this->model->where([
            'id' =>$param['id'],
            'is_del'=>0,
            'status'=>0
        ])->select()->toArray();

        if (!empty($result)) {
            #更新记录
            $update = $this->model->where('id', '=', $param['id'])->update([
                'status' => 1
            ]);
            if ($update) {
                #尝试记录操作日志 不影响主流程
                try {
                    $this->logger->logOperation($param['id'], 'Message', '处理', '处理站内信');
                } catch (\Exception $e) {
                    #记录日志或其他操作
                }
                return $this->success();
            } else {
                return $this->error(500001, '操作失败');
            }

        } else {
            #未找到记录返回失败
            return $this->error(400000, '未找到相关数据');
        }
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-02-13 11:03:36
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum($field = 'all')
    {
        $res = $this->model->getFieldEnum($field);
        return [
            'code' => 0,
            'data' => $res
        ];
    }

}